package com.sixmac.controller;

import com.obs.services.ObsClient;
import com.sixmac.common.base.BaseController;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.service.ReportService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sixmac.model.Report;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

import static com.sixmac.core.constant.ConstCurd.KEY_MAIN;

/**
 * ReportController
 * 2018-06-10 15:46:15
 *
 * @author Blade
 */
@Controller
@RequestMapping("/report")
public class ReportController extends BaseController {
    private static String CODE = "report";
    private static String PREFIX = "t_report";
    private static String LIST_SOURCE = "report.list";
    private static String BASE_PATH = "/platform/report/";

    @Autowired
    ReportService service;

    @RequestMapping(KEY_MAIN)
    public String index(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "report.html";
    }

    @RequestMapping(KEY_ADD)
    public String add(ModelMap mm) {
        mm.put("code", CODE);
        return BASE_PATH + "report_add.html";
    }

    @RequestMapping(KEY_EDIT + "/{id}")
    public String edit(@PathVariable Integer id, ModelMap mm) {
        Report report = service.findById(id);
        mm.put("model", JsonKit.toJson(report));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "report_edit.html";
    }

    @RequestMapping(KEY_VIEW + "/{id}")
    public String view(@PathVariable Integer id, ModelMap mm) {
        Report report = service.findById(id);
        mm.put("model", JsonKit.toJson(report));
        mm.put("id", id);
        mm.put("code", CODE);
        return BASE_PATH + "report_view.html";
    }

    @ResponseBody
    @RequestMapping(KEY_LIST)
    public Object list() {
        Object grid = paginate(LIST_SOURCE);
        return grid;
    }

    @ResponseBody
    @RequestMapping(KEY_SAVE)
    public AjaxResult save() {
        Report report = mapping(PREFIX, Report.class);
        boolean temp = service.save(report);
        if (temp) {
            return success(SAVE_SUCCESS_MSG);
        } else {
            return error(SAVE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_UPDATE)
    public AjaxResult update() {
        Report report = mapping(PREFIX, Report.class);
        boolean temp = service.update(report);
        if (temp) {
            return success(UPDATE_SUCCESS_MSG);
        } else {
            return error(UPDATE_FAIL_MSG);
        }
    }

    @ResponseBody
    @RequestMapping(KEY_REMOVE)
    public AjaxResult remove(@RequestParam String ids) {
        int cnt = service.deleteByIds(ids);
        if (cnt > 0) {
            return success(DEL_SUCCESS_MSG);
        } else {
            return error(DEL_FAIL_MSG);
        }
    }

    @Test
    public void uploadFile() {
        String endPoint = "obs.cn-east-2.myhwclouds.com";
        String ak = "JSG91U5FMOQUK7BHWCCC";
        String sk = "ztnum0YlqbNZeNRnXD7UwXExlEtCwwzOj1FuNrHY";
        // 创建ObsClient实例
        ObsClient obsClient = new ObsClient(ak, sk, endPoint);
        obsClient.putObject("obs-50b9", "video/ceshi/haha/", new File("C:\\Users\\admin\\Downloads\\最近很火奖.zip"));


// 使用访问OBS

// 关闭obsClient
        try {
            obsClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


}
